package yz.cp.back.config.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import yz.cp.back.auth.sign.SignRequestUtils;
import yz.cp.back.common.res.ApiResult;
import yz.cp.back.common.utils.ServletUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SignInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(request.getParameterMap().size()==0){
            return true;
        }
        try {
            boolean flag = SignRequestUtils.valid(request);
            if (!flag) {
                ServletUtils.renderResult(response, ApiResult.Custom().failure("校验失败"));
            }
            return flag;
        } catch (Exception e) {
            ServletUtils.renderResult(response, ApiResult.Custom().failure(e.getMessage()));
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
